package com.smfx.yxt.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.enums.LeaveStatus;
import com.smfx.yxt.common.enums.WorkApprovalType;
import com.smfx.yxt.daoSqlserver.AttachmentListMapper;
import com.smfx.yxt.daoSqlserver.OrganizeGroupMapper;
import com.smfx.yxt.daoSqlserver.TeacherInfoMapper;
import com.smfx.yxt.daoSqlserver.TeacherLeaveMapper;
import com.smfx.yxt.daoSqlserver.WorkApprovalConfMapper;
import com.smfx.yxt.model.StudentLeave;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.TeacherLeave;
import com.smfx.yxt.model.WorkApprovalConf;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.AttachRelationReqVO;
import com.smfx.yxt.reqVo.TeacherLeaveReqVO;
import com.smfx.yxt.service.ITeacherLeaveService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.view.TeacherInfoView;
import com.smfx.yxt.view.TeacherLeaveView;
import com.smfx.yxt.view.TermView;
import com.smfx.yxt.wechat.utils.TemplateUtil;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;

@Service("teacherLeaveService")
public class TeacherLeaveServiceImpl implements ITeacherLeaveService {

	@Autowired
	private TeacherLeaveMapper teacherLeaveMapper;
	@Autowired
	private AttachmentListMapper attachmentListMapper;
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	@Autowired
	private WorkApprovalConfMapper workApprovalConfMapper;
	@Autowired
	private OrganizeGroupMapper organizeGroupMapper;
	
	@Override
	public int insertSelective(TeacherLeave record) {
		return teacherLeaveMapper.insertSelective(record);
	}

	@Override
	public TeacherLeave selectByPrimaryKey(Integer id) {
		return teacherLeaveMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(TeacherLeave record) {
		return teacherLeaveMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<TeacherLeaveView> selectListByPage(Page<TeacherLeaveView> page) {
		return teacherLeaveMapper.selectListByPage(page);
	}

	@Override
	public int insert(TeacherLeaveReqVO reqVO) {
		teacherLeaveMapper.insertSelective(reqVO);
		if(reqVO.getIsAttach() != null) {
			AttachRelationReqVO attach = new AttachRelationReqVO();
			attach.setRelationId(Long.valueOf(reqVO.getId()));
			attach.setAttachIds(reqVO.getIds());
			attachmentListMapper.updateRelationIdByIds(attach);
		}
		TeacherInfo teacher = teacherInfoMapper.selectByPrimaryKey(reqVO.getApproverId());
		String reqUrl = Const.BASEPATH + Const.TEACHER_ROLE + MenuConst.TEACHER_LEAVE;
		TemplateUtil.teacherLeaveApplySend(teacher.getOpenid2(), teacher.getName(), 
				reqVO.getTeacherName(), reqVO.getStartTime(), reqVO.getEndTime(), 
				reqVO.getLeaveReason(), reqVO.getLeaveTypeText(), reqUrl);
		return 1;
	}

	@Override
	public int updateAudit(TeacherLeave record) {
		record.setUpdateTime(new Date());
		int count = teacherLeaveMapper.updateByPrimaryKeySelective(record);
		if(count > 0) {
			
		}
		return count;
	}

	class TeacherLeaveNoticeExecutor implements Runnable {
		
		private TeacherLeave leave;
		
		
		public TeacherLeaveNoticeExecutor(TeacherLeave leave) {
			this.leave = leave;
		}

		@Override
		public void run() {
			try {
				String auditResult = "";
				if(leave.getLeaveStatus().equals(LeaveStatus.AUDIT_SUCCESS.getCode())) {
					auditResult = "已批准";
				}else {
					auditResult = "已拒绝：" + leave.getRejectReason();
				}
				String reqUrl = Const.BASEPATH + Const.TEACHER_ROLE + MenuConst.TEACHER_LEAVE;
				TeacherInfo teacher = teacherInfoMapper.selectByPrimaryKey(leave.getTeacherId());
				TeacherInfoView view = BeanUtils.copyAs(teacher, TeacherInfoView.class);
				TemplateUtil.leaveAuditSend(teacher.getOpenid2(), "您的请假申请得到批复！", auditResult, leave.getApprover(), 
						leave.getUpdateTime(), reqUrl);
				if(leave.getLeaveStatus().equals(LeaveStatus.AUDIT_SUCCESS.getCode())) {
					WorkApprovalConf conf = new WorkApprovalConf();
					conf.setItemCode(WorkApprovalType.TEACHER_LEAVE.getCode());
					conf.setSchoolId(leave.getSchoolId());
					conf = workApprovalConfMapper.selectByParam(conf);
					List<String> array = Arrays.asList(conf.getCcerId().split(","));
					List<TeacherInfo> list = teacherInfoMapper.selectListByIds(array);
					String desc = view.getGradeText() + view.getName();
					for(TeacherInfo teacherInfo : list) {
						TemplateUtil.leaveAuditSend(teacherInfo.getOpenid2(), desc + "老师的请假申请得到批复！", auditResult, leave.getApprover(), 
								leave.getUpdateTime(), reqUrl);
					}
					list = organizeGroupMapper.selectSubjectGradeTeacher();
					for(TeacherInfo info : list) {
						TemplateUtil.leaveAuditSend(info.getOpenid2(), desc + "老师的请假申请得到批复，请各位行政、级长、科长知悉", auditResult, leave.getApprover(), 
								leave.getUpdateTime(), reqUrl);
					}
				}
			} catch (Exception e) { 
	            e.printStackTrace(); 
	        }
		}
	}

}
